namespace DotNetNuke.Services.Personalization
{
    using DotNetNuke.Common;
    using DotNetNuke.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web;

    public class PersonalizationController
    {
        public PersonalizationInfo LoadProfile(int UserId, int PortalId)
        {
            PersonalizationInfo objPersonalization = new PersonalizationInfo();
            objPersonalization.UserId = UserId;
            objPersonalization.PortalId = PortalId;
            objPersonalization.IsModified = false;
            IDataReader dr = DataProvider.Instance().GetProfile(UserId, PortalId);
            if (dr.Read())
            {
                objPersonalization.Profile = Globals.DeserializeHashTableXml(dr["ProfileData"].ToString());
            }
            else
            {
                DataProvider.Instance().AddProfile(UserId, PortalId);
                objPersonalization.Profile = new Hashtable();
            }
            dr.Close();
            return objPersonalization;
        }

        public void LoadProfile(HttpContext objHTTPContext, int UserId, int PortalId)
        {
            objHTTPContext.Items.Add("Personalization", this.LoadProfile(UserId, PortalId));
        }

        public void SaveProfile(PersonalizationInfo objPersonalization)
        {
            this.SaveProfile(objPersonalization, objPersonalization.UserId, objPersonalization.PortalId);
        }

        public void SaveProfile(PersonalizationInfo objPersonalization, int UserId, int PortalId)
        {
            if ((objPersonalization != null) && objPersonalization.IsModified)
            {
                string ProfileData = Globals.SerializeHashTableXml(objPersonalization.Profile);
                DataProvider.Instance().UpdateProfile(UserId, PortalId, ProfileData);
            }
        }

        public void SaveProfile(HttpContext objHTTPContext, int UserId, int PortalId)
        {
            PersonalizationInfo objPersonalization = (PersonalizationInfo) objHTTPContext.Items["Personalization"];
            this.SaveProfile(objPersonalization, UserId, PortalId);
        }
    }
}

